package com.ssm.controller;

import com.ssm.entity.UserEntity;
import com.ssm.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.springframework.ui.Model;

import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/user")
//参考资料
//https://blog.csdn.net/nba_linshuhao/article/details/82799685
//https://blog.csdn.net/jjs15259655776/article/details/82695277
//https://www.cnblogs.com/qianjinyan/p/10341710.html
//https://blog.csdn.net/xsj_blog/article/details/83008883
//解决jstl标签识别问题https://blog.csdn.net/shenhaibb_/article/details/83543373
//https://blog.csdn.net/u012926354/article/details/82689253 ssm增删查改
//https://blog.csdn.net/LitongZero/article/details/81269274 ssm增删查改
//https://www.jianshu.com/p/2339174a4ff3 ssm增删查改
//如出现java.lang.NoClassDefFoundError说明运行时目录和编译时目录不一致
//https://www.jianshu.com/p/c4b8fba020f2

public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/showUser")
    public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        long userId = Long.parseLong(request.getParameter("id"));
        UserEntity userEntity = this.userService.selectUser(userId);
        ObjectMapper mapper = new ObjectMapper();
        response.getWriter().write(mapper.writeValueAsString(userEntity));
        response.getWriter().close();
    }

    //用户列表
    @RequestMapping("/userList")
    public ModelAndView userList(){
        String viewName = "user/user_list";
        ModelAndView modelAndView = new ModelAndView(viewName);
        List<UserEntity> users = this.userService.userList();
        modelAndView.addObject("users",users);
        return modelAndView;
    }


    // 添加用户页面
    @RequestMapping("/addUser")
    public ModelAndView addUser() {
        String viewName = "user/add_user";//视图名
        ModelAndView modelAndView = new ModelAndView(viewName);
        return modelAndView;
    }

    // 添加用户
    @RequestMapping("/addUser/post")
    public ModelAndView addUserPost(UserEntity userEntity) {
        ModelAndView mav = new ModelAndView();  //创建一个jsp页面对象
        try {
            userService.addUser(userEntity);
            mav.setViewName("redirect:/user/userList");
            return mav;
        } catch (Exception e) {
            mav.addObject("message", e.getMessage());
            mav.setViewName("user/add_user");
            return mav;
        }
    }

    //删除用户
    @RequestMapping("/delUser")
    public String delUser(HttpServletRequest request, Model model) {
        long id = Long.parseLong(request.getParameter("id"));
        try {
            userService.delUser(id);
        } catch (Exception e) {
            model.addAttribute("message", e.getMessage());

        }
        return "redirect:/user/userList";
    }

    //更新用户页面
    @RequestMapping("/updateUser")
    public String updateUser(HttpServletRequest request, Model model) {
        long id = Long.parseLong(request.getParameter("id"));
        UserEntity user = userService.selectUser(id);
        model.addAttribute("user", user);
        return "user/update_user";
    }

    //更新用户
    @RequestMapping("/updateUser/post")
    public ModelAndView updateUserPost(UserEntity userEntity) {
        ModelAndView mav = new ModelAndView();  //创建一个jsp页面对象
        try {
            userService.updateUser(userEntity);
            mav.setViewName("redirect:/user/userList");
            return mav;
        } catch (Exception e) {
            mav.addObject("message", e.getMessage());
            mav.setViewName("user/update_user");
            return mav;
        }
    }

}

